<template>
	<view>
		<image src="../../../static/gitee.png" mode="widthFix" style="width: 250rpx;height:250rpx;margin-left: 250rpx;margin-top: 100rpx;"></image>
		
		<view class="inputarea">
			<input type="text" v-model="username" value="" placeholder="邮箱" class="uni-input"/>
		</view>
		
		<view class="inputarea">
			<input type="password" v-model="password" value="" placeholder="密码" class="uni-input"/>
		</view>
		
		<view class="btns" >
			<button @click="login" type="default" :disabled="loading">{{loading ? '登陆中' : '登陆'}}</button>
		</view>
	</view>
	
	
</template>

<script>
	export default {
		data() {
			return {
				client_secret : '1b1c309f8610f956cebb9959e5259ca0d0faf3be2326f7485e04a041ddd1b3d5',
				client_id : '0986fbc9ccb70f0850be9c57a902765d6cc9625a2cec37c3a6c7b5f77bb7bc83',
				username : '',
				password : '',
				loading : false,
			}
		},
		onLoad() {
			let tokenv5 = uni.getStorageSync("access_token")
			let tokenv3 = uni.getStorageSync("private_token")
			if(tokenv3 && tokenv5){
				uni.switchTab({
					url:"../../mine/mine"
				})
			}
			uni.setNavigationBarTitle({
				title: "登陆"
			})
		},
		methods: {
			login (){
				this.loading = true;
				this.v3Login();
			},
			v3Login(){
				uni.request({
					url:"https://gitee.com/api/v3/session",
					method : "POST",
					data : {email:this.username,password : this.password},
					success: (res) => {
						if(res.statusCode == 401){
							uni.showToast({
								icon:"none",
								title:"用户名或密码错误"
							})
						}else if(res.statusCode == 304 || res.statusCode == 200 || res.statusCode == 201){
							uni.setStorageSync('private_token',res.data.private_token)
							uni.setStorageSync('user_id',res.data.id)
							uni.setStorageSync('username',res.data.username)
							// v5 登陆
							this.v5Login()
						}else{
							uni.showToast({
								icon:"none",
								title:"未知错误"
							})
						}
					},
					error:(res)=>{
						this.clearLogin()
						uni.showToast({
							icon:"none",
							title:"请求验证失败 , 请稍后再试"
						})
					},
					complete: () => {
						this.loading = false
						uni.hideLoading()								
					}
				})
			},
			v5Login(){
				uni.request({
					url:"https://gitee.com/oauth/token",
					method : "POST",
					data : {
						username:this.username,
						password : this.password,
						grant_type:"password",
						client_id : this.client_id,
						client_secret : this.client_secret,
						scope:"user_info projects pull_requests issues notes gists enterprises"
					},
					success: (res) => {
						if(res.statusCode == 401){
							uni.showToast({
								icon:"none",
								title:"用户名或密码错误"
							})
						}else if(res.statusCode == 304 || res.statusCode == 200 || res.statusCode == 201){
							uni.setStorageSync('access_token',res.data.access_token)
							uni.showToast({
								icon:"success",
								title:"登录成功"
							})
							uni.switchTab({
								url:"../../mine/mine"
							})
						}else{
							uni.showToast({
								icon:"none",
								title:"未知错误"
							})
						}
					},
					error:(res)=>{
						uni.showToast({
							icon:"none",
							title:"请求验证失败 , 请稍后再试"
						})
						this.clearLogin()
					},
					complete: () => {
						this.loading = false
						uni.hideLoading()								
					}
				})
			},
			clearLogin(){
				uni.setStorageSync("private_token","")
				uni.setStorageSync("access_token","")
				uni.setStorageSync('user_id',"")
				uni.setStorageSync('username',"")
			}
			
		}
	}
</script>

<style lang="less">
.inputarea{
	width: 690rpx;
	margin-left: 30rpx;
	margin-top: 30rpx;
	input{
		font-size: 40rpx;
		border-radius: 20rpx;
	}
}

.btns{
	margin-top: 60rpx;
	width: 590rpx;
	margin-left: 80rpx;
	button{
		border-radius: 30rpx;
	}
}
</style>
